import { _decorator, BoxCollider2D, CircleCollider2D, Collider2D, Component, Contact2DType, ICollisionEvent, Input, input, ITriggerEvent, Node, PhysicsSystem2D, RigidBody2D,Animation, Vec2 } from 'cc';
import { loading } from './loading';
import { store } from './store';
import { AudioManager } from './AudioManager';
const { ccclass, property } = _decorator;

@ccclass('BirdCollide')
export class BirdCollide extends Component {
    // 刚体组件 
    public birdRgd:RigidBody2D = null
    // 动画组件
    public birdAni:Animation = null
    // 当前游戏游玩的节点 scroe: 得分 state: 游戏状态 
    public gameLoading = new loading()
    protected onEnable(): void {
        // 注册触发器事件
        this.node.getComponent(Collider2D).on(Contact2DType.BEGIN_CONTACT,this.judgeType,this)
    }
    // 根据碰撞的类型来判断与修改得分以及游戏的状态
    judgeType(selfCollider:Collider2D,otherCollider:Collider2D){
        let tag = otherCollider.node.getComponent(Collider2D).tag
        if(tag === 1){
            // 播放音频
            AudioManager.instance.playEffect('point')
            //  加分
            store.score ++
        }else{
            // 注册触发器事件
            this.node.getComponent(Collider2D).off(Contact2DType.BEGIN_CONTACT,this.judgeType,this)
            // 播放音频
            AudioManager.instance.playEffect('hit')
            // 游戏结束
            loading.gameState = false
        }
    }

}


